package com.yunupmall.service.admin.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.KindPo;
import com.yunupmall.api.fordVO.KindVO;
import com.yunupmall.api.fordentity.Commodity;
import com.yunupmall.api.fordentity.Kind;
import com.yunupmall.mapper.commoditymapper.KindMapper;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.service.admin.service.KindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class Kindimpl extends ServiceImpl<KindMapper,Kind> implements KindService {
    @Autowired
    private KindMapper mapper;
    @Autowired
    private CommodityService commodityService;

    public List<Kind> getKindName() {
        return mapper.getKindName();
    }

    /**
     *根据ID获取类别数据
     */
    @Override
    public List<Kind> byid(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            throw new RuntimeException("ID为空");
        }
        LambdaQueryWrapper<Kind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Kind ::getId,id);
        wrapper.eq(Kind ::getDeflag,0);
        return this.list(wrapper);
    }

    /**
     * 新增商品类别
     * @param kind
     * @return
     */
    @Override
    @Transactional
    public String AddKind(Kind kind) {
        if (ObjectUtil.isEmpty(kind.getUpId())) {
            kind.setIsRank(1);
        }else {
            kind.setIsRank(2);
        }
        kind.setDeflag(0);
        kind.setCreateTime(LocalDate.now());
        if (this.save(kind)){
            return "新增成功";
        }else {
            return "新增失败";
        }

    }

    /**
     * 分页
     * @param kindPo
     * @return
     */
    @Override
    public Page<Kind> pageKind(KindPo kindPo) {
        LambdaQueryWrapper<Kind> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(kindPo.getId())) {
            wrapper.eq(Kind ::getId,kindPo.getId());
        }
        if (ObjectUtil.isNotEmpty(kindPo.getName())) {
            wrapper.like(Kind::getName,kindPo.getName());
        }
        wrapper.eq(Kind ::getDeflag,0);
        Page<Kind> kindPage = new Page<>(kindPo.getPagePO().getCurrent(), kindPo.getPagePO().getSize());
        Page<Kind> page = this.page(kindPage, wrapper);
        return page;
    }

    /**
     * 获取类别List
     * @param kindPo
     * @return
     */
    @Override
    public List<Kind> getList(KindPo kindPo) {
        LambdaQueryWrapper<Kind> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(kindPo.getId())){
            wrapper.eq(Kind ::getId,kindPo.getId());
        }
        if (ObjectUtil.isNotEmpty(kindPo.getName())) {
            wrapper.like(Kind ::getName,kindPo.getName());
        }
        wrapper.eq(Kind ::getDeflag,0);
        return  this.list(wrapper);
    }

    @Override
    public List<Kind> getAllKindData(Integer thissize, Integer page) {
        return mapper.getAllKindData(thissize, page);
    }


    @Override
    @Transactional
    public String deleteKindData(Integer id) {
        if (ObjectUtil.isEmpty(id)){
            throw new RuntimeException("ID为空");
        }
        LambdaQueryWrapper<Commodity> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Commodity ::getKindId,id);
        List<Commodity> list = commodityService.list(wrapper1);
        if (ObjectUtil.isNotEmpty(list)&&list.size() > 0){
            throw new RuntimeException("该类别下有商品无法删除");
        }
        LambdaUpdateWrapper<Kind> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Kind ::getId,id);
        Kind kind = new Kind();
        kind.setDeflag(1);
        if (this.update(kind,wrapper)){
            return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 查询第一等级商品
     * @return
     */
    @Override
    public List<Kind> getListone() {
        LambdaQueryWrapper<Kind> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Kind ::getIsRank,1);
        wrapper.eq(Kind ::getDeflag,0);
        return this.list(wrapper);
    }

    /**
     * 通过类别id分页查询商品信息
     * @param kindVO
     * @return
     */
    @Override
    public Page<Commodity> pageCommtity(KindVO kindVO) {
        if (ObjectUtil.isEmpty(kindVO.getId())){
            throw new RuntimeException("id为空");
        }
        LambdaQueryWrapper<Commodity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Commodity ::getKindId,kindVO.getId());
        wrapper.eq(Commodity ::getDeflag,0);
        wrapper.eq(Commodity::getStatics,0);
        Page<Commodity> commodityPage = new Page<>(kindVO.getPage(), kindVO.getLimit());
        Page<Commodity> page = commodityService.page(commodityPage, wrapper);
        return page;
    }

}
